package bookstore.servlet.admin;



import bookstore.entity.Product;
import bookstore.entity.User;
import bookstore.service.PorductService;
import bookstore.service.UserService;
import bookstore.service.impl.PorductServiceImp;
import bookstore.service.impl.UserServiceImpl;
import bookstore.servlet.BaseServlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * @author 苗庆
 * @version 1.0
 * 商品类
 */

//设置默认上传文件夹为：D:/imgUpload/
//指定缓存大小：1024 * 1024，单位:bytes
//单个上传文件最大大小:1024 * 1024 * 5，单位:bytes
//限制该multipart/form-data请求中数据的大小,默认是-1，表示没有限制，单位:bytes:1024 * 1024 * 5 * 5
@MultipartConfig(location = "D:/java/IDEA/training/img/bookimg", fileSizeThreshold = 1024 * 1024, maxFileSize = 1024 * 1024 * 5, maxRequestSize = 1024 * 1024 * 5 * 5)
@WebServlet("/admin/porduct.do")
public class PorductServlet extends BaseServlet {

    PorductService porductService=new PorductServiceImp();
    UserService userService=new UserServiceImpl();

    //查询所有商品信息
    public void queryAllGoods(HttpServletRequest request, HttpServletResponse response){

    }


    //添加商品信息
    public void addTOGoods(HttpServletRequest request, HttpServletResponse response) throws IOException {
        Product product = queryAll(request);
        int i = porductService.addTOGoods(product);
        PrintWriter writer = response.getWriter();

        //添加成功直接刷新页面
        if (i!=-1){
            writer.write("添加成功");
        }else { //添加失败
            writer.write("添加失败");
        }
    }



    //修改商品信息
    public void update(HttpServletRequest request, HttpServletResponse response) {

        }



    //获取模态框的信息
    public Product queryAll(HttpServletRequest request){
        //商品名称
        String productName = request.getParameter("productName");
        //商品价格
        Double price = Double.valueOf(request.getParameter("price"));
        //商品分类
        String category = request.getParameter("category");
        //商品数量
        Integer pNum = Integer.valueOf(request.getParameter("pNum"));
        //商品图片
        String imgUrl = request.getParameter("imgUrl");
        //商品描述
        String description = request.getParameter("description");

        System.out.println(productName);
        System.out.println(price);
        System.out.println(category);
        System.out.println(pNum);
        System.out.println(imgUrl);
        System.out.println(description);
        Product product = new Product(null,productName,price,category,pNum,imgUrl,description);
        return product;
    }



    //删除商品信息
    public void delete(HttpServletRequest request, HttpServletResponse response){

    }


    //管理员登陆,记住cookie
    public void AdminLogin(HttpServletRequest request, HttpServletResponse response) throws IOException {

    }


    //管理员登陆注销
    public void AdminLoginSession(HttpServletRequest request, HttpServletResponse response) throws IOException {

    }




        //
//    //上传商品图片
//    public void upFile(HttpServletRequest request, HttpServletResponse response){
//        //获取文件
//        try {
//            System.out.println("你好");
//            Part part = request.getPart("file");
//
//            System.out.println(part);
//
//            //获取文件后缀名 也就是文件类型
//            String realFileName = part.getSubmittedFileName();
//
//            System.out.println(realFileName);
//
//            String type = realFileName.substring(realFileName.lastIndexOf("."));
//
//            System.out.println(type);
//
//            //通过 UUID 生成随机文件名
//            String fileName = UUID.randomUUID().toString() + type;
//            //获取项目下该文件夹路径
//            System.out.println(fileName);
//
////            String path = "D:/java/IDEA/training/img/bookimg" + fileName;
////            System.out.println(p);
//        } catch (IOException e) {
//            e.printStackTrace();
//        } catch (ServletException e) {
//            e.printStackTrace();
//        }
//    }
}
